package com.aube.mis.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aube.apiv2.service.draw.DrawActivityV2VoService;
import com.aube.apiv2.vo.draw.DrawActivityV2Vo;
import com.aube.apiv2.vo.draw.DrawOptionV2Vo;
import com.aube.apiv2.vo.draw.MemberAddressV2Vo;
import com.aube.common.req.vo.PaginationReqVo;
import com.aube.common.resp.vo.PaginationRespVo;
import com.aube.mis.dto.LotteryDTO;
import com.aube.mis.dto.LotteryOptionDTO;
import com.aube.mis.dto.LotteryWinnerDTO;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.PrizeDTO;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.LotteryService;
import com.aube.mis.service.ResourceService;
import com.aube.mis.vo.DataTableCriterias;
import com.aube.support.ResultCode;

@SuppressWarnings("unchecked")
@Service
public class LotteryServiceImpl extends AbstractService implements LotteryService {

	private static Logger log = LoggerFactory.getLogger(LotteryServiceImpl.class);

	@Resource
	DrawActivityV2VoService drawActivityV2VoService;

	@Resource
	ResourceService resourceService;

	@Override
	public List<LotteryDTO> getLotteryList(DataTableCriterias criterias, String showId) {
		PaginationReqVo<DrawActivityV2Vo> reqVo = fillPaginationInfo(criterias);
		reqVo.setShowid(showId);
		ResultCode<PaginationRespVo<DrawActivityV2Vo>> serverResp = drawActivityV2VoService.queryList(reqVo);
		List<LotteryDTO> resultList = (List<LotteryDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public LotteryDTO transform(Object object) {
				return convertLotteryDTO((DrawActivityV2Vo) object);
			}
		});
		return resultList;
	}
	
	@Override
	public LotteryDTO getLotteryDetail(String activityId) {
		ResultCode<DrawActivityV2Vo> result = drawActivityV2VoService.getDetailById(Long.parseLong(activityId));
		LotteryDTO lotteryDTO = convertLotteryDTO(result.getRetval());
		ResultCode<String> ruleContent = drawActivityV2VoService.getDrawRuleByActivityId(Long.parseLong(lotteryDTO.getShowId()), Long.parseLong(lotteryDTO.getActivityId()));
		lotteryDTO.setRuleContent(ruleContent.getRetval());
		return lotteryDTO;
	}

	@Override
	public LotteryDTO editLottery(LotteryDTO lotteryDTO) {
		DrawActivityV2Vo vo = new DrawActivityV2Vo();
		vo.setActivityName(lotteryDTO.getName());
		vo.setEndTime(new Timestamp(lotteryDTO.getEndTime()));
		vo.setStartTime(new Timestamp(lotteryDTO.getStartTime()));
		if(StringUtils.isNotEmpty(lotteryDTO.getActivityId())) {
			vo.setId(Long.parseLong(lotteryDTO.getActivityId()));
		}
		vo.setPicurl(lotteryDTO.getPicUrl());
		vo.setShowid(Long.parseLong(lotteryDTO.getShowId()));
		vo.setYtip(lotteryDTO.getLotteryAvailableTip());
		vo.setNtip(lotteryDTO.getLotteryNotAvailableTip());
		vo.setLoginTip(lotteryDTO.getLotteryLoginTip());
		if (CollectionUtils.isNotEmpty(lotteryDTO.getOptionList())) {
			List<DrawOptionV2Vo> optionList = (List<DrawOptionV2Vo>) CollectionUtils.collect(lotteryDTO.getOptionList(), new Transformer() {

				@Override
				public DrawOptionV2Vo transform(Object input) {
					LotteryOptionDTO dto = (LotteryOptionDTO) input;
					DrawOptionV2Vo result = new DrawOptionV2Vo();
					result.setActivityId(Long.parseLong(dto.getActivityId()));
					result.setDisplayName(dto.getDisplayName());
					if(StringUtils.isNotEmpty(dto.getOptionId())) {
						result.setId(Long.parseLong(dto.getOptionId()));
					}
					result.setPicurl(dto.getPicUrl());
					if(StringUtils.isNotEmpty(dto.getPrizeId())) {
						result.setPrizeId(Long.parseLong(dto.getPrizeId()));
					}
					result.setPrizeName(dto.getPrizeName());
					result.setPrizeStock(dto.getPrizeStock());
					result.setProbability(new BigDecimal(dto.getProbability().toString()).multiply(new BigDecimal("100")).intValue());
					result.setRemainStock(dto.getRemainStock());
					return result;
				}
			});
			vo.setOptionVoList(optionList);
		}
		ResultCode<DrawActivityV2Vo> result = drawActivityV2VoService.saveOrModify(vo);
		drawActivityV2VoService.saveDrawRuleByActivityId(result.getRetval().getId(), lotteryDTO.getRuleContent());
		return convertLotteryDTO(result.getRetval());
	}
	
	@Override
	public void deleteLottery(String lotteryId) {
		drawActivityV2VoService.removeById(Long.parseLong(lotteryId));
	}

	private LotteryDTO convertLotteryDTO(DrawActivityV2Vo drawActivityV2Vo) {
		LotteryDTO dto = new LotteryDTO();
		dto.setActivityId(drawActivityV2Vo.getId().toString());
		dto.setStartTime(drawActivityV2Vo.getStartTime().getTime());
		dto.setEndTime(drawActivityV2Vo.getEndTime().getTime());
		dto.setName(drawActivityV2Vo.getActivityName());
		dto.setPicUrl(drawActivityV2Vo.getPicurl());
		dto.setShowId(drawActivityV2Vo.getShowid().toString());
		dto.setLotteryAvailableTip(drawActivityV2Vo.getYtip());
		dto.setLotteryNotAvailableTip(drawActivityV2Vo.getNtip());
		dto.setLotteryLoginTip(drawActivityV2Vo.getLoginTip());
		if (CollectionUtils.isNotEmpty(drawActivityV2Vo.getOptionVoList())) {
			List<LotteryOptionDTO> optionList = (List<LotteryOptionDTO>) CollectionUtils.collect(drawActivityV2Vo.getOptionVoList(), new Transformer() {

				@Override
				public LotteryOptionDTO transform(Object input) {
					DrawOptionV2Vo vo = (DrawOptionV2Vo) input;
					LotteryOptionDTO result = new LotteryOptionDTO();
					result.setActivityId(vo.getActivityId().toString());
					result.setDisplayName(vo.getDisplayName());
					result.setOptionId(vo.getId().toString());
					result.setPicUrl(vo.getPicurl());
					if(vo.getPrizeId() != null) {
						result.setPrizeId(vo.getPrizeId().toString());
					}
					result.setPrizeName(vo.getPrizeName());
					result.setPrizeStock(vo.getPrizeStock());
					result.setProbability(new BigDecimal(vo.getProbability()).divide(new BigDecimal(100)).doubleValue());
					result.setRemainStock(vo.getRemainStock());
					return result;
				}
			});
			dto.setOptionList(optionList);
		}
		return dto;
	}

	@Override
	public PaginationResultDTO<LotteryWinnerDTO> getWinnerList(DataTableCriterias criterias, String showId, String activityId) {
		PaginationReqVo<Long> reqVo = fillPaginationInfo(criterias);
		reqVo.setReq(Long.parseLong(activityId));
		reqVo.setShowid(showId);
		ResultCode<PaginationRespVo<MemberAddressV2Vo>> serverResp = drawActivityV2VoService.queryWinnerList(reqVo);
		List<LotteryWinnerDTO> resultList = (List<LotteryWinnerDTO>)CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public LotteryWinnerDTO transform(Object input) {
				LotteryWinnerDTO result = new LotteryWinnerDTO();
				MemberAddressV2Vo vo = (MemberAddressV2Vo) input;
				try {
					BeanUtils.copyProperties(result, vo);
				} catch (Exception e) {
					e.printStackTrace();
				}
				PrizeDTO prize = resourceService.getPrizeDetail(vo.getPrizeId().toString());
				result.setPrize(prize);
				return result;
			}
		});
		PaginationResultDTO<LotteryWinnerDTO> result = new PaginationResultDTO<LotteryWinnerDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}
}
